#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int sum(int x)
{
    int ret = 0;
    while (x)
    {
        ret += x % 10;
        x /= 10;
    }
    return ret;
}

int addDigits(int num) 
{
    int mid = 0;
    int ret = num;
    while (ret > 9)
    {
        ret = sum(ret);
    }
    return ret;
}